package com.yellowpage.omc.ability.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.omc.ability.data.Partner;
import com.yellowpage.omc.ability.dao.PartnerDao;

public class PartnerServiceImpl extends BaseServiceImpl implements PartnerService {
	  private PartnerDao partnerDao;
	
	  public void setPartnerDao(PartnerDao partnerDao) {
		  this.partnerDao = partnerDao;
	  }
	
	
	  /* Smooth generate Method */ 
	  public Partner getPartner(Long id){
	      return partnerDao.get(id);
	  }
	  
    public List<Partner> getAllPartner(){
        return partnerDao.getAll();
    }
    
    public List<Map> queryPartnerByPage(Map queryMap, PageRender pageRender) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"SELECT a.PARTNER_ID , a.PARTNER_CODE , a.PARTNER_KEY , a.CONTACT_NAME , a.CONTACT_TEL , a.CONTACT_ADDRESS , a.CONTACT_NOTE , " +
				"       a.TECH_CONTACT_NAME , a.TECH_CONTACT_TEL , a.TECH_CONTACT_NOTE , a.NOTE , " +
				"       (select display_name from CA_USER where user_id = a.LAST_UPDATE_USER) as last_update_user, " +
				"       (select display_name from CA_USER where user_id = a.CREATE_USER) as create_user, " +
				"       a.CREATE_TIME , a.LAST_UPDATE_TIME , a.ADDRESS , a.NAME , a.STATUS , (SELECT display_name FROM CA_USER WHERE user_id = a.LAST_SYNCHRONIZATION_USER) AS last_synchronization_user,a.LAST_SYNCHRONIZATION_TIME FROM OMC_PARTNER a WHERE 1=1 ")
				    			.addCondition(" and PARTNER_ID = :partnerId ")
							    			.addCondition(" and PARTNER_CODE like :partnerCode ")
							    			.addCondition(" and NAME like :name ")
							    			.addCondition(" and PARTNER_KEY = :partnerKey ")
							    			.addCondition(" and CONTACT_NAME = :contactName ")
							    			.addCondition(" and CONTACT_TEL = :contactTel ")
							    			.addCondition(" and CONTACT_ADDRESS = :contactAddress ")
							    			.addCondition(" and CONTACT_NOTE = :contactNote ")
							    			.addCondition(" and TECH_CONTACT_NAME = :techContactName ")
							    			.addCondition(" and TECH_CONTACT_TEL = :techContactTel ")
							    			.addCondition(" and TECH_CONTACT_NOTE = :techContactNote ")
							    			.addCondition(" and NOTE = :note ")
							    			.addCondition(" and CREATE_USER = :createUser ")
							    		    .addCondition(" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime ")
		    				    			.addCondition(" and LAST_UPDATE_USER = :lastUpdateUser ")
							    		    .addCondition(" and date_format(LAST_UPDATE_TIME,'%Y-%m-%d') =  :lastUpdateTime ")
		    				    			.addCondition(" and ADDRESS = :address ")							    			
							    			.addCondition(" and STATUS = :status ")
							    			.addCondition(" and LAST_SYNCHRONIZATION_USER = :LastSynchronizationUser ")
							    			.addCondition(" and LAST_SYNCHRONIZATION_TIME = :LastSynchronizationTime ");
		
		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
    
    
    public Map getPartnerMap(){
    	Map dataSourceMap = new LinkedMap();
    	
    	QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
		" SELECT a.NAME as NAME, a.PARTNER_ID as VALUE  FROM OMC_PARTNER a,OMC_PARTNER_INTERFACE b WHERE a.PARTNER_ID=b.PARTNER_ID AND b.STATUS = '1';  ")
					    ; 	
         
		List<Map> resultList = sqlAssistant.query(
				baseDao.getConnection(), new HashMap());
				
		
		for(int i = 0; i < resultList.size(); i++){
			dataSourceMap.put(resultList.get(i).get("VALUE"), resultList.get(i).get("NAME"));
		}
		
		return dataSourceMap;
    }
    
    
	public void savePartner(Partner partner) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("select sn.seq_num as seq_num from OMC_SEQ_NUM sn where sn.seq_name = 'OMC_PARTNER_CODE' ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		Integer codeNum = (Integer) resultList.get(0).get("SEQ_NUM");
		
       try{
    		
		    baseDao.getConnection().createStatement().execute("update  OMC_SEQ_NUM sn " +
		    		"   set sn.seq_num = " + (codeNum.intValue() + 1) + 
		    		"  where sn.seq_name = 'OMC_PARTNER_CODE'");
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		String code = "";
		
		for(int i = 0; i < (6 - codeNum.toString().length()); i++ ){
			code = code + "0";
		}
		code = code + 	codeNum.toString();

		partner.setPartnerCode("P" + code);

		sqlAssistant = baseDao
				.newQueryAssistantSQL("select sn.seq_num as seq_num from OMC_SEQ_NUM sn where sn.seq_name = 'OMC_PARTNER_KEY' ");
		resultList = sqlAssistant.query(baseDao.getConnection(), new HashMap());

		Integer keyNum = (Integer) resultList.get(0).get("SEQ_NUM");
		
         try{
    		
		    baseDao.getConnection().createStatement().execute("update  OMC_SEQ_NUM sn " +
		    		"   set sn.seq_num = " + (codeNum.intValue() + 1) + 
		    		"  where sn.seq_name = 'OMC_PARTNER_KEY'");
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		String key = "";
		for(int i = 0; i < (12 - keyNum.toString().length()); i++ ){
			key = key + "0";
		}
		
		key = key + keyNum.toString();

		partner.setPartnerKey(key);

		partnerDao.save(partner);
	}
    
	  public void updatePartner(Partner partner){
	      partnerDao.update(partner);
	  }
	  
	  public void delete(List deleteList){
	      partnerDao.remove(deleteList);
	  }
	  
	  
	 
	
}	